<?php namespace App\Models;

use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 学校院系
 * Class SchoolDept
 *
 * @package App\Models
 * @property int $id
 * @property string $name 名称
 * @property int $school_id 学校id
 * @property string $created_at
 * @property string $updated_at
 * @property-read \App\Models\School $school
 * @method static \Illuminate\Database\Query\Builder|\App\Models\SchoolDept whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\SchoolDept whereSchoolId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\SchoolDept whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\SchoolDept whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\SchoolDept whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class SchoolDept extends Model
{
    use ValidatingTrait;
    use SoftDeletes;

    const CACHE_TAG = 'school_dept';
    
    protected $table = 'xyj_schools';
    
    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name',
        'tid',
        'doctors',
    ];

    protected $rules = [
        'name'      => 'required',
        'tid' => 'required|integer',
      //  'doctors' => 'required',
    ];

    protected $validationMessages = [
        'name.required'      => '名称必填',
        'tid.required' => '学校id必填',
        'tid.integer'  => '学校id必须为整数',
        'doctors' => '硕博士点必填',
        
    ];

    /**
     * 所属学校
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function school()
    {
        return $this->belongsTo('\App\Models\School', 'school_id');
    }

    /**
     * 用于select的列表
     * @return array
     */
    public static function getListData($schoolId)
    {
        return \Cache::tags(self::CACHE_TAG)->rememberForever('school_depts_' . $schoolId, function () use ($schoolId) {
            $infos = self::where('tid', '=', $schoolId)->get();
            $listData = ['0' => '请选择'];
            foreach ($infos as $info) {
                $listData[$info->id] = $info->name;
            }
            return $listData;
        });
    }

    /**
     * 用于select的列表的json格式
     * @return array
     */
    public static function getListDataJson($schoolId)
    {
        return \Cache::tags(self::CACHE_TAG)->rememberForever('school_depts_json_' . $schoolId, function () use ($schoolId) {
            return self::where('school_id', '=', $schoolId)->select(['id', 'name'])->get()->toJson();
        });
    }
    
    

    /**
     * 用于select的学院列表
     * @return array
     */
    public static function getDeptList( $schoolId )
    {
        $result = \Cache::tags(self::CACHE_TAG)->rememberForever('new-dept-list:' . $schoolId, function () use ($schoolId) {
            $infos = self::where('tid','=',$schoolId)->where('level',2)->select(['id','name'])->get()->toArray();
            return json_encode($infos, JSON_UNESCAPED_UNICODE );
        });
        return json_decode($result, true);
    }
    
    

    /**
     * 清除缓存
     */
    public function clearCache()
    {
        \Cache::tags(School::CACHE_TAG)->flush();
        \Cache::tags(self::CACHE_TAG)->flush();
    }
}